package com.java.test.system.registrationtemplate;

import com.java.test.system.registrationtemplate.dto.RegistrationTemplateAddDTO;
import com.java.test.system.registrationtemplate.dto.RegistrationTemplateQueryDTO;
import com.java.test.system.registrationtemplate.dto.RegistrationTemplateUpdateDTO;
import com.java.test.system.registrationtemplate.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 注册模板DAO
 *
 * @author Administrator
 * @date 2020/6/29 0029
 */
@Component
public class RegistrationTemplateDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<RegistrationTemplateEntity> getSelectByList(RegistrationTemplateQueryDTO queryDTO) {
        StringBuffer sql = new StringBuffer("select * from sys_registration_template where 1=1");
        if (queryDTO.getStatus()==1){
            sql.append(" and status = 1 ");
        }

        List<RegistrationTemplateEntity> list = jdbcTemplate.query(sql.toString(),new RegistrationTemplateEntity());

        return list;
    }

    /**
     * 查询地区ByIds
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<AreaEntity> getAreaListByIds(String ids) {
        String sql = "select * from sys_area where id in("+ids+")";
        List<AreaEntity> list = new ArrayList<AreaEntity>();
        try {
            list= jdbcTemplate.query(sql,new AreaEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return list;
    }

    /**
     * 查询字段ByIds
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<FieldEntity> getFieldListByIds(String ids) {
        String sql = "select * from sys_registration_template_field where id in("+ids+")";
        List<FieldEntity> list = new ArrayList<FieldEntity>();
        try {
            list= jdbcTemplate.query(sql,new FieldEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return list;
    }

    /**
     * 查询第三方ByIds
     * @param ids
     * @return
     */
    public List<TrilateralEntity> getTrilateralListByIds(String ids) {
        String sql = "select * from sys_registration_template_trilateral where id in("+ids+")";
        List<TrilateralEntity> list = new ArrayList<TrilateralEntity>();
        try {
            list= jdbcTemplate.query(sql,new TrilateralEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return list;
    }

    /**
     * 查询地区
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<AreaEntity> selectByListArea() {
        String sql = "select * from sys_area";

        List<AreaEntity> list = jdbcTemplate.query(sql,new AreaEntity());

        return list;
    }

    /**
     * 查询字段
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<FieldEntity> selectByListField(String ids) {
        String sql = "select * from sys_registration_template_field where id in("+ids+")";

        List<FieldEntity> list = jdbcTemplate.query(sql,new FieldEntity());

        return list;
    }

    /**
     * 查询第三方
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<TrilateralEntity> selectByListTrilateral() {
        String sql = "select * from sys_registration_template_trilateral";

        List<TrilateralEntity> list = jdbcTemplate.query(sql,new TrilateralEntity());

        return list;
    }

    /**
     * 查询注册类型
     * @author 
     * @date 2020/7/16 0016 16:14 
     * @return java.util.List<com.java.test.system.registrationtemplate.entity.TypeEntity>
     */
    public List<TypeEntity> selectToListType() {
        String sql = "select * from sys_registration_template_type";

        List<TypeEntity> list = jdbcTemplate.query(sql,new TypeEntity());

        return list;
    }

    /**
     * 查询注册类型ById
     * @author
     * @date 2020/7/16 0016 16:14
     * @return java.util.List<com.java.test.system.registrationtemplate.entity.TypeEntity>
     */
    public List<TypeEntity> selectToListTypeById(String id) {
        String sql = "select * from sys_registration_template_type where id = ?";

        List<TypeEntity> list = jdbcTemplate.query(sql,new TypeEntity(),id);

        return list;
    }

    /**
     * 获取数据BYName
     * @param name
     * @return
     */
    public RegistrationTemplateEntity getByGName(String name) {
        String sql = "select * from sys_registration_template where name =?";
        RegistrationTemplateEntity info = null;
        try {
            info= jdbcTemplate.queryForObject(sql, new RegistrationTemplateEntity(), name);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 获取数据BYid
     * @param id
     * @return
     */
    public RegistrationTemplateEntity getById(String id) {
        String sql = "select *  from sys_registration_template where id = ?";
        RegistrationTemplateEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new RegistrationTemplateEntity(), id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return info;
    }

    /**
     * 保存
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public void insertSave(RegistrationTemplateAddDTO addDTO) {
        String sql ="insert into sys_registration_template(name,type,remarks,operation,createTime,status,area_id,field_id,trilateral_id) " +
                "values (?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,addDTO.getName(),addDTO.getType(),addDTO.getRemarks(),
                addDTO.getOperation(),addDTO.getCreateTime(),addDTO.getStatus(),
                addDTO.getAreaId(),addDTO.getFieldId(),addDTO.getTrilateralId());
    }

    /**
     * 判断当前名称的数据是否存在
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public RegistrationTemplateEntity getByKeyExcludeId(String name, String id) {
        String sql = "select * from sys_registration_template where name = ? and id <> ?";
        RegistrationTemplateEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new RegistrationTemplateEntity(),name,id);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 修改
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public void updateById(RegistrationTemplateUpdateDTO updateDTO) {
        String sql = "update sys_registration_template " +
/*
                "set  name = ?,type = ?,area_id = ?,field_id = ?,trilateral_id = ? ,remarks = ?,modifyTime=?,status=? " +
*/
                "set  field_id = ?,trilateral_id = ? ,modifyTime=?" +
                "where id = ?";

        jdbcTemplate.update(sql, updateDTO.getFieldId(),updateDTO.getTrilateralId(),updateDTO.getModifyTime(),updateDTO.getId());
    }

    /**
     * 删除
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public void deleteById(String id) {
        String sql ="DELETE FROM sys_registration_template WHERE id = ?";
        jdbcTemplate.update(sql,id);
    }

    /**
     * 修改状态
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public int updateByIdToStatus(RegistrationTemplateUpdateDTO updateDTO) {
        String sql = "update sys_registration_template " +
                "set  status = ? where id = ?";

        return jdbcTemplate.update(sql, updateDTO.getStatus(),updateDTO.getId());
    }

    /**
     * 查询类型ById
     * @author
     * @date 2020/7/16 0016 16:39
     * @return java.util.List<com.java.test.system.registrationtemplate.entity.TypeEntity>
     */
    public TypeEntity getTypeListByIds(int type) {
        String sql = "select *  from sys_registration_template_type where id = ?";
        TypeEntity info = null;
        try {
            info = jdbcTemplate.queryForObject(sql, new TypeEntity(), type);
        }catch (EmptyResultDataAccessException e){
            return null;
        }

        return info;
    }
}
